(function($){
	var KEY = "jforest.paging";

	var Paging = function(element,options){
		this.options = $.extend({
			pageNo:1,			//当前页数
			pageTotal:1,		//页面数量
			pageLimit:20,		//页面数据限制量
			dataCount:0,		//数据数量
			dataTotal:0,		//数据总数量
			displayNumber:5,
			nextEventNumber:null,
			prevEventNumber:null,
			callback:function(options){}
		},options);
		this.arrayEl = [];
		if(!this.options.nextEventNumber || this.options.nextEventNumber>this.options.displayNumber){
			this.options.nextEventNumber = Math.round(this.options.displayNumber/3)*2-1; //三分之一
		}
		if(!this.options.prevEventNumber || this.options.prevEventNumber>this.options.displayNumber){
			this.options.prevEventNumber = Math.round(this.options.displayNumber/3);
		}
		$(element).append(this.createEl());this.setData();
	}
	
	Paging.prototype = {
		createEl:function(){
			var paging = this;this.$el = $('<ul class="pagination"></ul>');
			this.previous = {pageNo:0,$el:$('<li><a href="javascript:void(0);">&laquo;</a></li>')
				.click(function(event){if($(this).hasClass('disabled')){return;};paging.setData({pageNo:paging.previous.pageNo});
					paging.options.callback.apply(paging,[paging.options]);})}
			this.$el.append(this.previous.$el);
			for(var i=0;i<this.options.displayNumber;i++){
				this.arrayEl[i] = $('<li><a href="javascript:void(0);">'+(i+1)+'</a></li>').data('number',i).data('pageNo',(i+1))
				.click(function(event){if($(this).hasClass('active')){return;};
					paging.setData({pageNo:$(this).data('pageNo')});paging.options.callback.apply(paging,[paging.options]);});
				this.$el.append(this.arrayEl[i]);
			}
			this.next = {pageNo:2,$el:$('<li><a href="javascript:void(0);">&raquo;</a></li>')
				.click(function(event){if($(this).hasClass('disabled')){return;};paging.setData({pageNo:paging.next.pageNo});
					paging.options.callback.apply(paging,[paging.options]);})}
			this.$el.append(this.next.$el);
			return this.$el;
		},
		setData:function(options){
			var flag = options?this.options.pageNo == options.pageNo:false;
			this.options = $.extend(this.options,options || {});
			var opts = this.options;
			if(!opts.pageTotal){opts.pageTotal=1;}
			if(opts.pageTotal==1 && opts.dataTotal>opts.pageLimit){
				var i = opts.dataTotal%opts.pageLimit>0?1:0;
				opts.pageTotal = Math.floor(opts.dataTotal/opts.pageLimit) + i;
			}
			if(opts.pageNo>opts.pageTotal){opts.pageNo=opts.pageTotal;}else if(opts.pageNo<1){opts.pageNo=1;}
			//计算PageNo位置
			var startNo = this.arrayEl[0].data('pageNo'),index=opts.pageNo-startNo+1;
			if(index>opts.displayNumber||index<1||index>opts.nextEventNumber||index<opts.prevEventNumber||startNo+opts.displayNumber-1>opts.pageTotal){
				var no = opts.pageNo-opts.prevEventNumber;if(index>opts.nextEventNumber){no=opts.pageNo-opts.nextEventNumber;}
				if(no+opts.displayNumber-1>opts.pageTotal){no=opts.pageTotal-opts.displayNumber+1;};if(no<1){no=1;}
				this.previous.pageNo=no-1;this.next.pageNo=opts.pageNo+1;
				for(var i=0;i<this.arrayEl.length;i++){
					var $el = this.arrayEl[i].data('pageNo',(no+i));$el.find('>a').html((no+i));
					if((no+i)>opts.pageTotal){$el.hide();}else{$el.show();};if((no+i)==opts.pageNo){$el.addClass('active');}else{$el.removeClass('active')}
				}
			}else if(flag == false){
				for(var i=0;i<this.arrayEl.length;i++){
					var $el = this.arrayEl[i],no=$el.data('pageNo');
					if((no)>opts.pageTotal){$el.hide();}else{$el.show();};if((no)==opts.pageNo){$el.addClass('active');}else{$el.removeClass('active')}
				}
			}
			if((this.next.pageNo=this.options.pageNo+1)>this.options.pageTotal){this.next.$el.addClass('disabled');}else{this.next.$el.removeClass('disabled');}
			if((this.previous.pageNo=this.options.pageNo-1)<1){this.previous.$el.addClass('disabled');}else{this.previous.$el.removeClass('disabled');}
		}
	}
	
	$.fn.jfPaging = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				value=new Paging(this,opts);$this.data(KEY,value); //创建控件
			}else if (typeof(args[0]) === "string"){
				var entity = $this.data(KEY);
                if(entity === undefined){return;}
                value = entity[args[0]].apply(entity, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfPaging.Constructor = Paging;
	
})(window.jQuery)